package com.fanrenqiji.modules.sys.service.impl;

import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.service.BaseCommonService;
import com.fanrenqiji.common.utils.StringUtils;
import com.fanrenqiji.modules.sys.dao.BlockListDao;
import com.fanrenqiji.modules.sys.entity.BlockList;
import com.fanrenqiji.modules.sys.entity.Dict;
import com.fanrenqiji.modules.sys.service.BlockListService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class BlockListCommonServiceImpl extends BaseCommonService implements
		BlockListService {

	@Autowired
	private BlockListDao blockListDao;

	@Override
	public BlockList findOne(Long id) {
		return blockListDao.findOne(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(BlockList blockList) {
		blockListDao.save(blockList);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(Long id) {
		blockListDao.deleteById(id);
	}

	@Override
	public Page<BlockList> find(Page<BlockList> page, BlockList blockList) {
		DetachedCriteria dc = blockListDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(blockList.getName())) {
			dc.add(Restrictions.eq("name", blockList.getName()));
		}
		if (StringUtils.isNotEmpty(blockList.getMobile())) {
			dc.add(Restrictions.eq("mobile", blockList.getMobile()));
		}
		if (StringUtils.isNotEmpty(blockList.getIdCard())) {
			dc.add(Restrictions.eq("idCard", blockList.getIdCard()));
		}
		dc.add(Restrictions.eq(Dict.DEL_FLAG, Dict.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return blockListDao.find(page, dc);
	}

	@Override
	public String checkMobileOrIdCard(String condition) {
		if (!StringUtils.isEmpty(condition)
				&& blockListDao.findByMobileOrIdCard(condition,condition) == null) {
			//白名单
			return "true";
		}
		//黑名单
		return "false";
	}

}
